﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using yiCommerce.Areas.Admin.Models.ProductVariants;
using yiCommerce.Areas.Admin.Models.Products;
using yiCommerce.Attributes;
using yiCommerce.Controllers;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Securitys;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.Securitys;

namespace yiCommerce.Areas.Admin.Controllers
{

    [AdminAuthorize(Order = 1)]
    [AdminProductAuthorize(Order = 2)]
    public class ProductController :BaseController 
    {

        private IPermissionService _permissionService;
        private IProductSearchService _productSearchService;
        private IProductService _productService;
        private IPictureService _pictureService;
        private IWorkContext _workContext;

        public ProductController(IWorkContext workContext,IPermissionService permissionService, IProductSearchService productSearchService, IProductService productService, IPictureService pictureService)
        {
            _workContext = workContext;
            _permissionService = permissionService;
            _productSearchService = productSearchService;
            _productService = productService;
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
          

            var shopId = this.GetShopId();
            var products = _productSearchService.SearchProducts(shopId, 0, 0, string.Empty,ProductSortingEnum.CreatedOn,  0, 30);
            var model = new ProductSearchModel()
                {

                    ProductListModels = ToProductListModel(products)
                };
            ViewBag.Model = model; 

            return View();
        }

        public ActionResult Create()
        {
            var model = new ProductModel()
                {

                    PicUrl = _pictureService.GetDefaultPictureUrl(100)
                };


            ViewBag.Model = model;

            return View();
        }

        public ActionResult Edit(int productId , string tab)
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct))
            //{
            //    return AccessDeniedView();
            //}

            var p = _productService.GetProductById(productId);

            var model = new ProductModel()
                {

                    Id = p.Id,
                    ProductName = p.ProductName,
                    ShortDescription = p.ShortDescription,
                    FullDescription = p.FullDescription,

                    ProductPictures = ToProductPictureModels(p.ProductPictures),
                    IsPublished = p.Published,

                    ProductVariants = ToProductVariantModels(p.ProductVariants),
                    ProductSpecificationAttributes=ToProductSpecificationAttributes(p.ProductSpecificationAttributes),


                    DefaultUploadPicture = new ProductPictureModel()
                        {
                            PictureUrl = _pictureService.GetDefaultPictureUrl(100)
                        }
                };

            ViewBag.Model = model;
            ViewBag.Tab = string.IsNullOrEmpty(tab) ? "pInfo" : tab;
            return View("Edit");

        }

        private IList<ProductListModel> ToProductListModel(IList<Product> products )
        {
            if (products == null)
            {
                return null;
            }
            else
            {
                return products.Select(c => new ProductListModel()
                    {
                        ProductId = c.Id,
                        IsPublished = c.Published,

                        ProductName = c.ProductName,
                        ProductListItemProductPicture = c.ProductPictures == null || c.ProductPictures.Count==0
                                                            ? new ProductListProductPictureModel()
                                                                {
                                                                    PicUrl = _pictureService.GetDefaultPictureUrl(50)
                                                                }
                                                            : new ProductListProductPictureModel()
                                                                {
                                                                    PicUrl =
                                                                        _pictureService.GetPictureUrl(
                                                                            c.ProductPictures.First().PictureId, 50, true)
                                                                }
                    }).ToList();
            }
        }
        private IList<ProductSpecificationAttributeListItemModel> ToProductSpecificationAttributes(
            ICollection<ProductSpecificationAttribute> productSpecificationAttributes)
        {
            if (productSpecificationAttributes == null)
            {
                return new List<ProductSpecificationAttributeListItemModel>();
            }
            else
            {
                return productSpecificationAttributes.Select(c => new ProductSpecificationAttributeListItemModel()
                    {
                        SpecificationAttributeName = c.SpecificationAttributeOption.SpecificationAttribute.Name,
                        SpecificationAttributeOptionName = c.SpecificationAttributeOption.Name
                    }).ToList();
            }
        }

        public IList<ProductPictureModel> ToProductPictureModels(ICollection<ProductPicture> productPictures)
        {

            if (productPictures == null)
            {
                return new List<ProductPictureModel>();
            }
            else
            {
                return productPictures.Select(c => new ProductPictureModel()
                {
                    ProductPictureId = c.Id,
                    PictureUrl = _pictureService.GetPictureUrl(c.Picture.Id,100,true),
                    PictureId = c.PictureId,
                    DisplayOrder = c.DisplayOrder,
                }).ToList();
            }
        }

        public IList<ProductVariantModel> ToProductVariantModels(ICollection<ProductVariant> productVariants)
        {

            if (productVariants == null)
            {
                return new List<ProductVariantModel>();
            }
            else
            {
                return productVariants.Select(c => new ProductVariantModel()
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Sku = c.Sku,
                        Price = c.Price,
                        StockQuantity = c.StockQuantity,
                        DisplayOrder = c.DisplayOrder,
                        Published = c.Published,

                    }).ToList();
            }
        }
    }
}
